package com.tian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tian.mapper.ReportMapperCustom;
import com.tian.pojo.Report;
import com.tian.pojo.bo.ReportBO;
import com.tian.pojo.bo.center.CenterUserBO;
import com.tian.pojo.vo.ReportVO;
import com.tian.pojo.vo.UserManageVO;
import com.tian.service.ReportService;
import com.tian.service.UserService;
import com.tian.utils.PagedGridResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private Sid sid;

    @Autowired
    private ReportMapperCustom reportMapperCustom;

    @Autowired
    private UserService userService;


    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Report insertReport(String username, String mobile, ReportBO reportBO) {

        String userid = userService.queryUserIdByUsername(username);
        Report report=new Report();
        report.setId(sid.nextShort());
        report.setReporttype(reportBO.getReporttype());
        report.setCreatetime(new Date());
        report.setUsermobile(mobile);
        report.setHandlestatus(0);
        report.setNote(reportBO.getNote());
        report.setUsername(username);
        report.setUserid(userid);

        reportMapperCustom.insertReport(report);

        return report;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedGridResult queryPagedReport(String userId,Integer page, Integer pageSize) {
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userId);

            PageHelper.startPage(page,pageSize);
            List<ReportVO> list = reportMapperCustom.queryPagedReport(map);

            for (ReportVO l:list){
                Integer handlestatus = l.getHandlestatus();
                Integer reporttype = l.getReporttype();
                if (handlestatus==1){
                    l.setHandlestatusStr("已处理");
                }else {
                    l.setHandlestatusStr("未处理");
                }

                if (reporttype==1){
                    l.setReporttypeStr("假冒与盗版");
                }
                if (reporttype==2){
                    l.setReporttypeStr("虚假交易");
                }
                if (reporttype==3){
                    l.setReporttypeStr("收到空包裹");
                }
                if (reporttype==4){
                    l.setReporttypeStr("滥发信息");
                }


            }

            return setterPagedGrid(list,page);

    }
    private PagedGridResult setterPagedGrid(List<?> list,Integer page){
        PageInfo<?> pageList =new PageInfo<>(list);
        PagedGridResult grid=new PagedGridResult();
        grid.setPage(page);
        grid.setRows(list);
        grid.setTotal(pageList.getPages());
        grid.setRecords(pageList.getTotal());
        return grid;
    }
}
